//
//  TaskFooter.m
//  RRQS
//
//  Created by kyjun on 16/3/24.
//
//

#import "TaskFooter.h"

@interface TaskFooter ()<AMapSearchDelegate>
@property(nonatomic,strong) UIButton* btnIcon;
@property(nonatomic,strong) UILabel* labelSendAddress;
@property(nonatomic,strong) UILabel* labelReGeocode;
@property (nonatomic, strong) AMapSearchAPI *search;
@end

@implementation TaskFooter

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0];
        [self layoutUI];
        [self layoutConstraints];
    }
    return self;
}

#pragma mark =====================================================  user inferface layout
-(void)layoutUI{
    [self addSubview:self.btnIcon];
    [self addSubview:self.labelSendAddress];
    [self addSubview:self.labelReGeocode];
}

-(void)layoutConstraints{
    NSArray* formats = @[ @"H:|-10-[btnIcon(==25)]", @"V:|-17-[btnIcon(==25)]", @"H:[btnIcon]-10-[labelSendAddress]-10-|", @"V:|-0-[labelSendAddress(==60)]", @"H:|-45-[labelReGeocode]-10-|", @"V:[labelSendAddress][labelReGeocode]-0-|"];
    NSDictionary* views = @{ @"btnIcon":self.btnIcon, @"labelSendAddress":self.labelSendAddress, @"labelReGeocode":self.labelReGeocode};
    for (NSString* format in formats) {
        NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
        [self addConstraints:constraints];
    }     
}

/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil)
    {
        self.labelReGeocode.text = response.regeocode.formattedAddress;
    }
}

#pragma mark =====================================================  property package
-(void)setEntity:(MOrder *)entity{
    if(entity){
        _entity = entity;
        MSubOrder* item = (MSubOrder*)[entity.arraySubOrder firstObject];
        self.labelSendAddress.text = entity.address;
        if([WMHelper isEmptyOrNULLOrnil:entity.addressRoad]){
        if([WMHelper isEmptyOrNULLOrnil:item.phoneAddress]){
         self.labelReGeocode.text =  @"";
        }else{
            NSArray* empty = [item.phoneAddress componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
            if(empty.count == 2){
                AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
                regeo.location = [AMapGeoPoint locationWithLatitude:[[empty lastObject] doubleValue] longitude:[[empty firstObject] doubleValue]];
                regeo.requireExtension = YES;
                regeo.radius = 1000;
                [self.search AMapReGoecodeSearch:regeo];
            }else{
              self.labelReGeocode.text =  @"";
            }
        }
        }else{
            self.labelReGeocode.text = entity.addressRoad;
        }
    }
}

- (void)setWaiMaiDodelHList:(WJWaiMaiDodelHList *)waiMaiDodelHList
{
    if(waiMaiDodelHList){
        _waiMaiDodelHList = waiMaiDodelHList;
        self.labelSendAddress.text = waiMaiDodelHList.send_address;
        self.labelReGeocode.text = waiMaiDodelHList.map_location;
    }
}
- (void)setWaiMaiDodelList:(WJWaiMaiDodelList *)waiMaiDodelList
{
    if(waiMaiDodelList){
        _waiMaiDodelList = waiMaiDodelList;
        self.labelSendAddress.text = waiMaiDodelList.sendaddress;
        self.labelReGeocode.text = waiMaiDodelList.address_road;
    }
}
-(UIButton *)btnIcon{
    if(!_btnIcon){
        _btnIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-light"] forState:UIControlStateNormal];
        _btnIcon.userInteractionEnabled = NO;
        _btnIcon.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _btnIcon;
}

-(UILabel *)labelSendAddress{
    if(!_labelSendAddress){
        _labelSendAddress = [[UILabel alloc]init];
        _labelSendAddress.font = [UIFont systemFontOfSize:14.f];
        _labelSendAddress.lineBreakMode = NSLineBreakByCharWrapping;
        _labelSendAddress.numberOfLines = 0;
        _labelSendAddress.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelSendAddress;
}

-(UILabel *)labelReGeocode{
    if(!_labelReGeocode){
        _labelReGeocode = [[UILabel alloc]init];
        _labelReGeocode.textColor = [UIColor grayColor];
        _labelReGeocode.font = [UIFont systemFontOfSize:12.f];
        _labelReGeocode.lineBreakMode = NSLineBreakByCharWrapping;
        _labelReGeocode.numberOfLines = 0;
        _labelReGeocode.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelReGeocode;
}

-(AMapSearchAPI *)search{
    if(!_search){
        _search = [[AMapSearchAPI alloc] init];
        _search.delegate = self;
    }
    return _search;
}

@end
